package com.dailyblue.java.project.smbms.stock.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dailyblue.java.commons.util.JsonResult;
import com.dailyblue.java.commons.util.ResultTool;
import com.dailyblue.java.project.smbms.stock.handler.StockExceptionHandler;
import com.dailyblue.java.project.smbms.stock.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;

@RestController
@RequestMapping("/stock")
@Slf4j
@CrossOrigin
public class StockController {
    @Resource
    private StockService stockService;

    @GetMapping("/{id}")
    public JsonResult find(@PathVariable("id") int id) {
        return ResultTool.success(stockService.getById(id));
    }

    @PutMapping("/{bid}/{num}")
    @SentinelResource(value = "update", fallback = "update", fallbackClass = StockExceptionHandler.class)
    public JsonResult update(@PathVariable("bid") int bid, @PathVariable("num") int num) throws SQLException {
        int count = stockService.updateStockNum(bid, num);
        if (count > 0) {
            return ResultTool.success(bid + "更新成功！");
        }
        return ResultTool.fail(bid + "更新失败！");
    }

}
